package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.ComponentsMapper;
import com.hnxxxy.workshop.model.ComponentsDO;
import com.hnxxxy.workshop.request.ComponentsPageRequest;
import com.hnxxxy.workshop.request.ComponentsRequest;
import com.hnxxxy.workshop.service.ComponentsService;
import com.hnxxxy.workshop.vo.ComponentsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 零件 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class ComponentsServiceImpl implements ComponentsService {

    @Resource
    private ComponentsMapper componentsMapper;

    @Override
    public void add(ComponentsRequest componentsRequest) {
        ComponentsDO componentsDO = new ComponentsDO();
        BeanUtils.copyProperties(componentsRequest, componentsDO);

        int rows = componentsMapper.insert(componentsDO);
        log.info("新增消息:rows={},data={}",rows,componentsDO);
    }

    @Override
    public void deleteById(int id) {
        int rows = componentsMapper.deleteById(id);
        log.info("删除消息条数 :rows={}",rows);
    }

    @Override
    public void update(ComponentsRequest componentsRequest) {
        ComponentsDO componentsDO = new ComponentsDO();
        BeanUtils.copyProperties(componentsRequest, componentsDO);

        int rows = componentsMapper.updateById(componentsDO);

        log.info("修改消息:rows={},data={}",rows,componentsDO);
    }

    @Override
    public ComponentsVO findByIdComponents(int id) {
        ComponentsVO componentsVO = new ComponentsVO();

        ComponentsDO componentsDO = componentsMapper.selectById(id);
        BeanUtils.copyProperties(componentsDO, componentsVO);
        log.info("根据id查询信息:id={}  depotVO={}",id,componentsVO);

        return componentsVO;
    }

    @Override
    public Map<String, Object> page(ComponentsPageRequest componentsPageRequest) {
        int page = componentsPageRequest.getPage();
        int size = componentsPageRequest.getSize();

        String name = componentsPageRequest.getName();


        QueryWrapper<ComponentsDO> queryWrapper = new QueryWrapper<ComponentsDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }


        Page<ComponentsDO> pageInfo = new Page<>(page, size);

        IPage<ComponentsDO> componentsDOIPage = componentsMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", componentsDOIPage.getTotal());
        pageMap.put("total_page", componentsDOIPage.getPages());
        pageMap.put("current_data",componentsDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;

    }

    private ComponentsVO beanProocess(ComponentsDO obj) {
        ComponentsVO componentsVO = new ComponentsVO();
        BeanUtils.copyProperties(obj, componentsVO);
        return componentsVO;
    }

}
